package com.Home.digital_life;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class VideoActivity extends ListActivity {
	List<Map<String, Object>> list;
	int c = 0;

	String url = "192.168.1.100";
	int port = 5000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		if (new FileUtils().isFileExist("video.xml") == false) {
			Toast.makeText(VideoActivity.this, "文件不存在！", Toast.LENGTH_SHORT)
					.show();
		} else {
			SimpleAdapter adapter = new SimpleAdapter(this, getData(),
					R.layout.music, new String[] { "title", "info" },
					new int[] { R.id.title, R.id.info });
			setListAdapter(adapter);
		}

	}

	private List<Map<String, Object>> getData() {
		list = new ArrayList<Map<String, Object>>();

		String filepath = new FileUtils().getSDPATH() + "video.xml";
		// File file=new File(filepath);
		InputStream instream = null;
		try {
			instream = new FileInputStream(new File(filepath));
			// int leng
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<MediaFile> medias = MediaFilePullParser.readXML(instream);
		Map<String, Object> map;
		int i;

		for (i = 0; i < medias.size(); i++) {
			map = new HashMap<String, Object>();
			map.put("title", medias.get(i).getName());
			map.put("info", medias.get(i).getStarring());
			list.add(map);
		}

		return list;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		c++;
		Log.v("MyListView4-click", (String) list.get(position).get("title"));
		String str = (String) list.get(position).get("title");
		byte[] commond = new byte[2];
		byte[] sn = str.getBytes();
		switch (c % 2) {
		case 0:
			commond[0] = 0;
			commond[1] = 5;
			Upload.TcpRun(url, port, commond, sn);
			break;
		case 1:
			commond[0] = 0;
			commond[1] = 7;
			Upload.TcpRun(url, port, commond, sn);
			break;

		}
		Toast.makeText(VideoActivity.this, "播放 " + str, Toast.LENGTH_SHORT)
				.show();
	}

}
